<?php
/**
 * --------------------------------
 * # 将阿拉伯数字钱数转化为中文大写钱数方案
 * --------------------------------
 * 1. 简介：将数字钱数【0.11】（最大为 100 万亿）转中文大写钱数【零圆壹角壹分】
 * 2. 目的：防止轻易伪造纸质文档中的数字，同时支持负数
 * 3. zhang
 * 4. 2021-10-21
 * --------------------------------
 * ## 更新日志 时间：2023-10-22
 * - 最新版本：https://github.com/Swordying/num2rmb.git
 * - 支持 composer：` $ composer require swordying/num2rmb `
 * --------------------------------
 */
function num2rmb($number = '',$start_mark = false){
    // 数字与中文数字对照表
    $tradition_number = array(
        0 => '零',
        1 => '壹',
        2 => '贰',
        3 => '叁',
        4 => '肆',
        5 => '伍',
        6 => '陆',
        7 => '柒',
        8 => '捌',
        9 => '玖',
    );
    // 中国计数进制换算单位
    $number_unit = array(
        0 => '',
        1 => '拾',
        2 => '佰',
        3 => '仟',
    );
    // 人民币浮点数单位
    $float_unit = array(
        0 => '分',
        1 => '角',
    );
    // 中国人民币单位
    $rmb_unit = array(
        0 => '圆',
        1 => '万',
        2 => '亿',
        3 => '兆', # 万亿
    );
    // 开始标识
    $start_mark = $start_mark ? '¥' : '';
    // 结束标识
    $end_mark = '整';
    // 正负标识
    $negative_mark = '负';
    if($number > 0){
        $negative_mark = '';
    }else{
        $number = abs($number);
    }
    // 数字格式整理
    $number = number_format($number,2,'.','');
    if($number == 0){
        return $start_mark.$tradition_number[0].$rmb_unit[0].$end_mark;
    }
    $number_array = explode('.',$number);
    // 整数部分数字
    $number_int = strrev($number_array[0]);

    // 整数部分数字长度
    $number_int_length = strlen($number_int);
    if($number_int_length > 15){
        return false; # '数字过大'
    }
    $tradition_rmb = '';
    if($number_array[1][0] == 0 && $number_array[1][1] == 0){
        $tradition_rmb = $end_mark;
    }else{
        if($number_array[1][0] == 0){
            $jiao = $tradition_number[0];
        }else{
            $jiao = $tradition_number[$number_array[1][0]].$float_unit[1];
        }
        $fen = $tradition_number[$number_array[1][1]].$float_unit[0];
        $tradition_rmb = $jiao.$fen;
    }

    if($number_int == 0){
        return $start_mark . $negative_mark . $tradition_number[0].$rmb_unit[0] . $tradition_rmb;
    }

    // 四位一组
    $number_int_group = [];
    $j = 0;
    for ($i = 0 ; $i < $number_int_length; $i++){
        if($i % 4 == 0 && $i != 0){
            $j ++;
        }
        $number_int_group[$j][] = (int) $number_int[$i];
    }

    // 转化后的整数部分
    $tradition_int = '';
    foreach($number_int_group as $k => $v){
        $group_unit = $rmb_unit[$k];
        $group_sum = array_sum($v);
        
        if($group_sum == 0){
            if($k == 0){
                $tradition_int = $rmb_unit[0];
            }
            continue;
        }
        $current_grpup_int = '';
        foreach($v as $key => $value){
            $current_tradition_number = $tradition_number[$value];
            $current_number_unit = '';
            if($key == 0 && $value == 0){
                $current_tradition_number = '';
            }
            if($value){
                $current_number_unit = $number_unit[$key];
            }
            $current_grpup_int = $current_tradition_number . $current_number_unit . $current_grpup_int;
        }
        $tradition_int = $current_grpup_int . $group_unit . $tradition_int;
    }
    // 去除多余的字符
    $tradition_int = str_replace(array(
        '零零零',
        '零零',
        '零亿',
        '零万',
        '零圆',
        '零兆',
        '兆',
    ),array(
        '零',
        '零',
        '亿',
        '万',
        '圆',
        '万亿',
        '万亿',
    ),$tradition_int);
    $result = $start_mark . $negative_mark . $tradition_int . $tradition_rmb;
    return $result;
}
